package top.hundreds.oasys.controller.FrontController;

import org.springframework.web.bind.annotation.*;
import top.hundreds.common.crud.bean.PageParam;
import top.hundreds.common.web.api.ApiResult;
import top.hundreds.oasys.bean.entity.Personnel;
import top.hundreds.oasys.bean.vo.PersonnelVo;
import top.hundreds.oasys.service.PersonnelService;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Personnel)表控制层
 *
 * @author lcf
 * @since 2021-01-29 19:18:51
 */
@RestController
@RequestMapping("/front/personnel")
public class PersonnelController {
    /**
     * 服务对象
     */
    @Resource
    private PersonnelService personnelService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/get")
    public ApiResult<PersonnelVo> getOne(Long id) {
        PersonnelVo personnelVo = this.personnelService.buildVo(this.personnelService.get(id));
        return ApiResult.success(personnelVo);
    }

    /**
     * 获取职位列表
     * @param pageParam 分页参数
     * @return 职位分页
     */
    @GetMapping("/list")
    public ApiResult<List<PersonnelVo>> list(PageParam<Personnel> pageParam) {
        List<PersonnelVo> list = this.personnelService.list(pageParam);
        return ApiResult.success(list);
    }

    /**
     * 编辑职位信息
     * @param personnel 职位信息
     * @return success
     */
    @PostMapping("/save")
    public ApiResult<String> save(@RequestBody Personnel personnel) {
        if (personnel.getId() != null) {
            this.personnelService.update(personnel);
        } else {
            this.personnelService.insert(personnel);
        }
        return ApiResult.success();
    }

    /**
     * 删除职位
     * @param id 职位id
     * @return success
     */
    @DeleteMapping("remove/{id}")
    public ApiResult<String> remove(@PathVariable("id") Long id){
        this.personnelService.delete(id);
        return ApiResult.success();
    }

    @GetMapping("/changeStatus/{id}/{status}")
    public ApiResult<String> changeStatus(@PathVariable Long id, @PathVariable Integer status) {
        if (id == null) {
            return ApiResult.fail("请传入参数");
        }
        this.personnelService.setStatus(id, status);
        return ApiResult.success();
    }
}
